/**
 * related to login.ui
 * 
 * @Author : wzgl5533
 * @Timestamp : 2017-07-18
 */
// 引入组件
var nf = sm("do_Notification");
var do_App = sm("do_App");
var do_Page = sm("do_Page");
var do_Global = sm("do_Global");
var do_Network = sm("do_Network");
var dojs = require("dojs");
var myjs = require("myjs/my");

// 声明变量
var root = ui("root");
var userEdt = ui("do_TextField_user");
var pwdEdt = ui("do_TextField_pwd");
var loginBtn = ui("do_Button_login");
// 获取初始化配置数据
var initData = do_Page.getData();
var currentOption = null;
// 引入标题栏，128px
root.add("topBar", "source://view/common/topbar.ui", 0, 0);
initView();
// 初始化布局效果
dojs.style.css(loginBtn, "dynamicButton");
dojs.style.css(ui("do_ALayout_reg"), "dynamicButton");
dojs.style.css(ui("do_ALayout_fetchPwd"), "dynamicButton");
dojs.page.allowHideKeyboard(loginBtn);
// 输入焦点处理
var edtFocus = require("source://view/login/test");
edtFocus.edtFocusEvent(userEdt, ui("do_Label_user"));
edtFocus.edtFocusEvent(pwdEdt, ui("do_Label_pwd"));

// -------------------------------------------------------------------------
function initView() {

	if (dojs.core.isNullData(initData)) {
		dojs.core.toast("初始化数据为空！");
	} else {
		currentOption = initData;
		// 初始化界面
		userEdt.hint = initData.user.hint;
		pwdEdt.hint = initData.password.hint;
		loginBtn.bgColor = do_Global.getMemory("btn_disable_bg_color");
		loginBtn.enabled = false;

		// 发消息,初始化标题栏
		do_Page.fire("initTopBarMsg", {
			text : dojs.core.isNullData(initData.title) ? "" : initData.title,
			isBack : initData.allowClose
		});
	}
}
// 点击登录按钮-----------------------------------------------------------
dojs.page.onTouch(loginBtn, function() {

	// 判断是否有网络
	if (dojs.core.valueEqual(do_Network.getStatus(), "none")) {
		dojs.core.toast("当前无网络连接，无法登录!");
		return;
	}
	if (dojs.core.isNullData(currentOption.onCallback))
		return;
	var pwd = pwdEdt.text;
	if (currentOption.password.encryption == "md5") {
		var md5 = require("tools/crypt/md5");
		pwd = md5.hex_md5(pwd).toLowerCase();
	}
	var loginCallback = require(currentOption.onCallback);
	loginCallback.invoke({
		user : userEdt.text,
		password : pwd,
		type : "login_normal"
	});
});
// 点击注册----------------------------------------------------------------
dojs.page.onTouch(ui("do_ALayout_reg"), function() {
	var entrance = require("mymodule/entrance");
	entrance.openReg.openRegPage();
});
//输入检测---------------------------------------------------------------------
userEdt.on("textChanged", function() {
	checkTextChange();
});
pwdEdt.on("textChanged", function() {
	checkTextChange();
});

// 检测信息是否输入完全，显示或禁止登录按钮
function checkTextChange() {
	if (!dojs.core.isNullData(userEdt.text)
			&& !dojs.core.isNullData(pwdEdt.text)) {
		loginBtn.bgColor = do_Global.getMemory("btn_enable_bg_color");
		loginBtn.enabled = true;
	} else {
		loginBtn.bgColor = do_Global.getMemory("btn_disable_bg_color");
		loginBtn.enabled = false;
	}
}
// 当前页面下，订阅android系统返回键的事件：3秒内连续点击两次退出应用
var canBack = false;
var delay3 = mm("do_Timer");

delay3.delay = 3000;
delay3.on("tick", function() {
	delay3.stop();
	canBack = false;
});
do_Page.on("back", function() {
	if (canBack) {
		do_Global.exit();
	} else {
		nf.toast("再次点击退出应用");
		canBack = true;
		delay3.start();
	}
});